
<template>    
    <div class="app-container">
        <el-row :gutter="10" class="mb8">            
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleCreate"
                    v-hasPermi="['demo/demoEntityExt/create']"
                    v-show="!disabled"
                >新增</el-button>
            </el-col>            
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['demo/demoEntityExt/delete']"
                    v-show="!disabled"
                >删除</el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="demoEntityExtList"
            @selection-change="handleSelectionChange"            
            @row-click="onClickRow"
        >            
            <el-table-column label="" width="65">
                <template slot-scope="scope">
                    <el-radio class="radio" v-model="referredId" :label="scope.row.id">&nbsp;</el-radio>
                </template>
            </el-table-column>            
            <!-- extend字段实体 -->            
            <el-table-column label="标题" align="center" prop="title" />            
            <el-table-column label="创建者" align="center" prop="creatorName" />            
            <el-table-column label="原型" align="center" prop="protoTitle" />            
            <el-table-column label="值3" align="center" prop="value3" />            
            <el-table-column label="明细" align="center">
                <template slot-scope="scope">
                    <span>{{ (scope.row.items||[]).map(v=>{return v.name}).join(",") }}</span>
                </template>
            </el-table-column>            
            <el-table-column label="相关人员" align="center">
                <template slot-scope="scope">
                    <span>{{ (scope.row.relevant||[]).map(v=>{return v.relevantName}).join(",") }}</span>
                </template>
            </el-table-column>            
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">                    
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row)"
                            v-hasPermi="['demo/demoEntityExt/edit']"
                            v-show="!disabled"                            
                    >修改</el-button>                    
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['demo/demoEntityExt/delete']"
                            v-show="!disabled"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>        
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />        
        <el-dialog title="新增" :visible.sync="createOpen" width="800px" append-to-body :close-on-click-modal="false">
            <createEditor ref="createEditor"                
            />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="createConfirm">确 定</el-button>
                <el-button @click="createCancel">取 消</el-button>
            </div>
        </el-dialog>        
        <el-dialog title="修改" :visible.sync="editOpen" width="800px" append-to-body :close-on-click-modal="false">
            <editEditor ref="editEditor"                
            />
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="editConfirm">确 定</el-button>
                <el-button @click="editCancel">取 消</el-button>
            </div>
        </el-dialog>        
    </div>
</template>
<script>
import {
    listDemoEntityExt,
    delDemoEntityExt,    
    listEntityId,    
    listCreatorId,    
    listProtoId,    
} from "@/api/demo/demoEntityExt";
export default {
    name: "DemoEntityExt",
    components:{        
        createEditor:()=>import('../create/form.vue'),        
        editEditor:()=>import('../edit/form.vue'),        
    },
    props: [        
        "disabled" // 禁用列表页面的增删改按钮
    ],
    data() {
        return {            
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 实体2表格数据
            demoEntityExtList: [],
            // 是否显示弹出层            
            createOpen: false,            
            editOpen: false,            
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,                
            },            
            referredId: null,
            referredRow: null,            
        };
    },
    created() {
        this.getList();        
        this.referredId = this.id;        
    },
    watch:{        
        id() {
            if (this.referredId != this.id) {
                this.referredId = this.id;
            }
        },        
    },
    methods: {        
        //关联m_demo_entity表选项
        getEntityIdItems() {
            this.getItems(listEntityId, {pageSize:10000}).then(res => {
                this.entityIdOptions = this.setItems(res, 'id', 'title')
            })
        },        
        //关联m_demo_person表选项
        getCreatorIdItems() {
            this.getItems(listCreatorId, {pageSize:10000}).then(res => {
                this.creatorIdOptions = this.setItems(res, 'id', 'name')
            })
        },        
        //关联m_demo_entity表选项
        getProtoIdItems() {
            this.getItems(listProtoId, {pageSize:10000}).then(res => {
                this.protoIdOptions = this.setItems(res, 'id', 'title')
            })
        },        
        /** 查询实体2列表 */
        getList() {
            this.loading = true;            
            listDemoEntityExt(this.queryParams).then(response => {                
                this.demoEntityExtList = response.data.list || [];
                this.total = response.data.total;                
                this.loading = false;
            });
        },        
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!=1
            this.multiple = !selection.length
        },        
        onClickRow(row) {
            this.referredId = row.id;
            this.referredRow = row;
            this.$emit("refer", row)
        },        
        handleCreate() {
            this.createOpen = true;
            this.$nextTick(()=>{
                this.$refs.createEditor.show({})
            })
        },        
        createConfirm: function() {            
            this.$refs.createEditor.submitForm(()=>{
                this.createOpen = false;
                this.getList();
            })            
        },
        // 取消按钮
        createCancel() {
            this.createOpen = false;
        },        
        handleEdit(row) {
            this.editOpen = true;
            this.$nextTick(()=>{
                this.$refs.editEditor.show(row)
            })
        },        
        editConfirm: function() {            
            this.$refs.editEditor.submitForm(()=>{
                this.editOpen = false;
                this.getList();
            })            
        },
        // 取消按钮
        editCancel() {
            this.editOpen = false;
        },        
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除实体2编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                return delDemoEntityExt(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function() {});
        },
    }
};
</script>
